//
//  StatusMenuVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/16.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit
import SVProgressHUD

class StatusMenuVC: RootVC {

    let titles = ["Tools",UserInfoManager.shared().isLogin ? "Logout":"Login","Restart MiFi","Scan the QR Code of the Wi-Fi network"]
    
    var scanHandle : (()->())?
    
    var labels : [UILabel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(changeStatus), name: Notification.Name.init("loginStatusDidChange"), object: nil)
    }
    
    override func configView() {
        view.backgroundColor = whiteColor
    }

    private func createBtns() {
        let btnHeight : CGFloat = 60
        let top = phoneTop == 0 ? 20 : phoneTop
        for (index,title) in titles.enumerated() {
            let label = UILabel(frame: CGRect(x: 8, y: top + btnHeight * CGFloat(index), width: CGFloat(Int(view.width * 0.75 - 16)), height: btnHeight))
            if index == 0 { label.textAlignment = .center }
            label.backgroundColor = whiteColor
            label.text = title
            label.numberOfLines = 0
            label.textColor = blackColor
            label.font = UIFont.systemFont(ofSize: 20)
            view.addSubview(label)
            
            let line = UIView(frame: CGRect(x: 0, y: label.height - 1, width: label.width, height: 1))
            line.backgroundColor = themeColor
            label.addSubview(line)
            
            let btn = UIButton(frame: label.frame)
            btn.backgroundColor = clearColor
            btn.addTarget(self, action: #selector(menuAction(sender:)), for: .touchUpInside)
            btn.tag = index
            view.addSubview(btn)
            labels.append(label)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        createBtns()
    }
    
    @objc private func menuAction(sender:UIButton) {
        guard sender.tag > 0 else {
            return
        }
        
        switch sender.tag {
        case 1:
            loginAction()
        case 2:
            restartDevice()
        case 3:
            scanAction()
        default:
            break
        }
    }
    
    private func loginAction() {
        if UserInfoManager.shared().isLogin {
            logoutAction()
        }else {
            guard checkConnect() else {
                return
            }
            checkLogin {}
        }
    }
    
    private func restartDevice() {
        checkLogin {
            net_restartDevice()
        }
    }
    
    private func scanAction() {
        if scanHandle != nil {
            scanHandle!()
        }
    }
    
    @objc private func changeStatus() {
        let title = UserInfoManager.shared().isLogin ? "Logout" : "Login"
        labels[1].text = title
    }
    
    private func logoutAction() {
        
        let alertVC = UIAlertController(title: "Alert", message: "Are you sure you want to quit?", preferredStyle: .alert)
       
        let action2 = UIAlertAction(title: "Cancel", style: .default) { (action) in
            
        }
        alertVC.addAction(action2)
        let action = UIAlertAction(title: "OK", style: .default) { (action) in
            net_logout()
        }
        alertVC.addAction(action)
        self.present(alertVC, animated: true, completion: nil)
        
      
    }
    
}
